<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    class Main extends CI_Controller {

        public function __construct() {
            parent::__construct();

            // Load the rest client spark
            $this->load->spark('restclient/2.1.0');

            // Run some setup
            $this->rest->initialize(array(
                'server' => REST_URL_HOTELS,
                'http_user' => 'cipa',
                'http_pass' => 'booking',
                'http_auth' => 'basic' // or 'digest' 
            ));

            $this->activeLang = $this->lang->mci_current();

            require_once EXTERNAL_URL . 'urlify.php';

            // Do caching
            $this->load->driver('cache', array('adapter' => 'file')); // za uklucuvanje na kes mogucnosti
        }

        public function index() {
            $this->home();
        }

        /*
         * Home page
         * 
         * @params : 
         *      input ('popular', 'visited', 'specialHotels', 'specialDests', 'recommended', 'googlemap','recentlybooked')
         */

        public function home() {


            $home_input = array('recommended', 'specialHotels', 'popular', 'recentlybooked', 'googlemap', 'visited');

            $file = cache_filename(array('lang' => $this->activeLang), 'combined');

            if (!$combined = $this->cache->get($file)) {

                $combined = $this->rest->post(
                        'home', array('lang' => $this->activeLang, 'input' => $home_input));

                if (DO_CACHE)
                    $this->cache->save($file, $combined, 300);
            }

            $data['recommended'] = $combined->recommended;
            $data['specialHotels'] = $combined->specialHotels;
            $data['popular'] = $combined->popular;
            $data['recentlybooked'] = $combined->recentlybooked;
            $data['googlemap'] = make_google_map($combined->googlemap);
            $data['visited'] = sort_attributes_visits($combined->visited);

            $data['seo_title'] = lang('title-home');
            $data['seo_description'] = lang('description-home');
            $data['seo_keywords'] = lang('keywords-home');

            $this->_load_top_offer_CMS($data);

            $data = currency_convert($data); // Convert currency

            $this->template->load('site/home', $data);
        }

        // TODO: Omoguciti kesiranje na metodi destinations_hotels_JSON
        public function destinations_hotels_JSON() {

            $rest_data = array(
                "name_startsWith" => $_GET['name_startsWith'],
                "lang" => $this->activeLang
            );

            $items = $this->rest->post("destinationsHotelsAutocomplete", $rest_data);

            //echo JSON to page  
            $response = $_GET["callback"] . "(" . json_encode(array('items' => $items)) . ")";

            echo $response;
        }

        /*
         * Search page
         * 
         * @params:
         *      check_in
         *      check_out
         *      nr_adults
         *      ages
         *      dest_hot
         * 
         */

        public function search() {

            if (isset($_GET['preview']))
                $rest_method = 'searchPreview';
            else
                $rest_method = 'search';

            $file = cache_filename($_GET, 'results');
            //
            if (!$data['results'] = $this->cache->get($file)) {

                $data['results'] = $this->rest->post($rest_method, array_merge($_GET, array('lowestPricePreview' => TRUE)));

                if (DO_CACHE)
                    $this->cache->save($file, $data['results'], 300);
            }
            if (isset($_GET['acc_type']))
                filter_acc_type($data['results'], $_GET['acc_type']);

            $items = $rest_method == 'search' ? make_sort_price_available($data['results']) : $data['results'];
            loadJoinedGroupsAttributes($data['results']);

            $data['items'] = json_encode($data['results']);
            $data['unique'] = findUniqueMakeCount($data['results']); // find unique filter attributes
            $data['unique_prices'] = findUniquePricesMakeCount($data['results'], isset($_GET['preview'])); // find unique prices for filter
            $data['unique_stars'] = findUniqueStarsMakeCount($data['results']); // find unique stars for filter
            // sort, splice and shuffle facilities & services
            sort_attributes($data['unique']);
            if (count($data['unique']) > 23)
                array_splice($data['unique'], 23);

            shuffle($data['unique']);

            // sort stars array
            ksort($data['unique_stars']);

            // load objects for maps
            $data['googlemap'] = make_google_map($data['results']);

            $data = currency_convert($data); // Convert currency

            $this->load->helper('search_seo');

            $data['seo_title'] = search_title();
            $data['seo_description'] = search_description();
            $data['seo_keywords'] = lang('keywords-home');
            $data['h1_title'] = search_h1();


            $this->template->load('site/results', $data);
        }

        private function _findRoom(&$details) {

            if (isset($details->rooms))
                foreach ($details->rooms as $index => $room) :
                    if ($room->room_ID == $_GET['room_id']):
                        $details->cur_room = $room;
                        break;
                    endif;
                endforeach;
        }

        /*
         * Object details with rooms and prices
         * 
         * @params:
         *      check_in
         *      check_out
         *      nr_adults
         *      ages
         *      hotel_id
         * 
         */

        public function accommodation($i) {

            $this->load->config('countries');

            if (isset($_GET['preview']))
                $rest_method = 'detailsPreview';
            else
                $rest_method = 'details';

            $id = explode('-', $i);
            $ide = $id[count($id) - 1];

            $_GET['hotel_id'] = $ide;

            $this->data['details'] = $this->rest->post($rest_method, $_GET);

            $this->_findRoom($this->data['details']); // Load current room
            // Make distances from available
            $this->data['distances_from'] = array();

            $dFromKey = array('Distances from', 'Udaljenosti objekta od', 'Расстояния от', 'Entfernungen von');

            if (isset($this->data['details']->facilities))
                foreach ($this->data['details']->facilities as $key => $value) :
                    if (in_array($key, $dFromKey)):

                        $this->data['distances_from'] = $this->data['details']->facilities->{$key};
                        unset($this->data['details']->facilities->{$key});
                    endif;

                endforeach;

            // load objects for maps
            $this->data['googlemap'] = make_google_map(array($this->data['details']));

            $this->data = currency_convert($this->data); // Convert currency

            $this->data['seo_title'] = $this->data['details']->seo_title != "" ? $this->data['details']->seo_title : $this->data['details']->object_name . ' | Cipa Travel';
            $this->data['seo_description'] = $this->data['details']->seo_description;
            $this->data['seo_keywords'] = $this->data['details']->seo_keywords;

            $this->load->helper('pricelist_table');
            $CI = &get_instance();
            price_list_table($CI, $this->data);

            $this->template->load('site/details', $this->data);
        }

        public function prices() {

            $this->data['details'] = $this->rest->post('detailsWithAvailbleInfo', $_GET);

            $this->data = currency_convert($this->data); // Convert currency

            if (isset($this->data['details']->accommNotAvailbale)):

                $error = $this->data['details'];

                switch ($error->message):

                    case $error->message == 'not_available' || $error->message == 'too_meny_person':
                        $msg = lang($error->message);
                        break;

                    case 'min_stay':
                        $msg = lang($error->message) . $error->value . ' ' . lang('days') . '!';
                        break;

                    case 'min_ps':
                        $msg = lang($error->message) . $error->value . ' ' . lang('person') . '!';
                        break;

                endswitch;

                $response = array(
                    'total' => 0,
                    'available' => FALSE,
                    'message' => $msg,
                    'value' => $this->data['details']->value,
                );

            else:

                $this->_findRoom($this->data['details'][0]); // Load current room

                foreach ($this->data['details'][0]->rooms as $index => $room) :

                    if ($room->room_ID == $_GET['room_id']):

                        $response = array(
                            'service_total' => $room->selling_total,
                            'tax_city' => isset($this->data['details'][0]->tax->total->city) ? $this->data['details'][0]->tax->total->city : 0,
                            'tax_insurance' => isset($this->data['details'][0]->tax->total->insurance) ? $this->data['details'][0]->tax->total->insurance : 0,
                            'tax_eco' => isset($this->data['details'][0]->tax->total->eco) ? $this->data['details'][0]->tax->total->eco : 0,
                            'total' => $room->total,
                            'available' => TRUE,
                            'message' => '',
                            'value' => ''
                        );

                        break;
                    endif;


                endforeach;

            endif;

            if (!isset($response))
                $response = array(
                    'total' => 0,
                    'available' => FALSE,
                    'message' => lang('not_available'),
                    'value' => '',
                );

            echo json_encode($response);
        }

        /*
         * Customer Form Show Object and room with prices
         * 
         * @params:
         *      check_in
         *      check_out
         *      nr_adults
         *      ages
         *      hotel_id
         *      room_id
         * 
         */

        public function reservation() {

            if (isset($_GET['currency']))
                unset($_GET['currency']);

            $data['reservation'] = $this->rest->post("customerForm", $_GET);

            $this->load->config('countries');

            $data['seo_title'] = lang('title-reservation_details');
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

            $this->load->helper('percent');
            $this->template->set_view('template_checkout');

            $cookie = array(
                'name' => 'cipa_cookie',
                'value' => '1',
                'expire' => '86500'
            );

            $this->input->set_cookie($cookie); // Set coockie for redirecting on confirmation page reload

            $this->template->load('site/checkout', $data);
        }

        /*
         * Customer Form Show Object and room with prices
         * 
         * @params:
         *      check_in
         *      check_out
         *      nr_adults
         *      ages
         *      hotel_id
         *      room_id
         * 
         */

        public function changedates() {

            $data['reservation'] = $this->rest->post("customerForm", $_GET);

            $data['seo_title'] = lang('title-change_dates');
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

            $this->template->set_view('template_checkout');
            $this->template->load('site/checkout_dates', $data);
        }

        /*
         * Create reservation write reservation to DB
         * 
         * method: createReservation
         * 
         * @params:
         *      check_in
         *      check_out
         *      nr_adults
         *      ages
         *      hotel_id
         *      room_id
         *      selling_type_ID
         *      payment_method
         *      payment_options
         *      note || remarks
         *      sex
         *      email
         *      name || firstname
         *      surname || lastname
         *      phone
         *      address
         *      city
         *      country
         *      zip
         * 
         */

        public function confirmation() {

            $coockie = get_cookie('cipa_cookie'); // Redirect if page reload

            if (!$coockie)
                redirect(lang_url());

            $rest_data = $_GET;
            $rest_data['agency'] = 'cipa';

            $data['reservation'] = $this->rest->post("createReservation", $rest_data);

            /*
             * Ovdje idemo na placanje preko payment gateway-a ako je:
             *
             *  -> metod placanja - Credit Card online
              -> selling type - Real Time
             */
            if ($_GET['payment_method'] === CREDIT_CARD_ONLINE && $_GET['selling_type_ID'] == 2):

                /* Prvo pronalazimo payment amount */
                $payment_info = $this->calculate_payment_amount($data['reservation']);

                // GO TO PAYMENT GATEWAY METHOD
                $this->go_to_payment($data['reservation']->reservation_id, $payment_info['amount'], 'ac-');

                return;

            endif;

            delete_cookie('cipa_cookie');

            $data['seo_title'] = lang('title-confirmation');
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

            $this->template->set_view('template_checkout');
            $this->template->load('site/checkout_thanks', $data);
        }

        public function generate_payment_link() {

            foreach ($_GET as $key => &$value)
                $_GET[$key] = urlencode($_GET[$key]);


            $_GET = array(
                'reservation_ID' => $_GET['reservation_ID'],
                'amount' => urlencode($_GET['amount']),
                'email' => $_GET['email'],
                'phone' => $_GET['phone'],
                'address' => $_GET['address'],
                'zip' => $_GET['zip'],
                'city' => $_GET['city'],
                'name' => $_GET['name'],
                'surname' => $_GET['surname']
            );

            $link = 'http://www.cipa-booking.com/en/go_to_payment/' . $_GET['reservation_ID'] . '/' . $_GET['amount'] . '?email=' . $_GET['email'] . '&phone=' . $_GET['phone'] . '&address=' . $_GET['address'] . '&zip=' . $_GET['zip'] . '&city=' . $_GET['city'] . '&name=' . $_GET['name'] . '&surname=' . $_GET['surname'];

            echo $link;
        }

        public function generate_link__K7m9MXhC() {

            $this->load->config('countries');

            $this->template->set_view('template_other');
            $data['seo_title'] = "Generate link";
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";
            $this->template->load('parts/generate_link', $data);
        }

        /*
         * Ovdje kreiramo inicijalizaciju za payment gateway
         */

        function go_to_payment($reservation_id, $amount, $what = 'ac-') {

            $cookie = array(
                'name' => 'cipa_cookie',
                'value' => '1',
                'expire' => '86500'
            );

            $this->input->set_cookie($cookie); // Set coockie for redirecting on confirmation page reload

            require_once EXTERNAL_URL . 'wpf-integrator.php';

            $return_url = lang_url('merchant_return_payment');

            // Look at your DemoMerchantInfo Link in the Implementation Packages Documentation to get the correct values for the following function
            $payment = new _ctpepost(
                    'ctpe.net', //Server
                    '/frontend/payment.prc', //Server-Path,
                    '8a8394c44204b416014207502c0603c2', //Sender
                    '8a8394c44204b41601420751970c03cb', //Channel
                    '8a8394c44204b416014207502c0703c6', //User-ID
                    'Spqbta84', //password
                    'test', //Token
                    'LIVE', //Transaction-Mode
                    'SYNC' //transaktion-Response
            );
            // set payment code (CC.DB for credit card, DD.DB for direct debit)
            $payment->setPaymentCode('CC.DB');
            // set payment information
            $payment->setPaymentInformation($amount, 'oder#' . $reservation_id . '/cipa-booking.com', 'reservation_#' . $reservation_id, 'EUR');
            // set customer contact information
            $payment->setCustomerContact($_GET['email'], $_GET['phone'], '192.123.151.1', '');
            // set customer address
            $payment->setCustomerAddress($_GET['address'], $_GET['zip'], $_GET['city'], '', 'ME');
            // set customer name
            $payment->setCustomerName('', '', $_GET['name'], $_GET['surname'], '');
            // set WPF parameter (the url is this script, this is important for redirecting to success and error page on the top of the script)
            $payment->setWPFparams('true', 'false', 'DEFAULT', 'EN', $return_url, $what . $reservation_id, "https://ssl.bookmontenegro.me/assets/css/omba.css");

            // commit payment
            $output = $payment->commitPOSTPayment();

            $processingresult = $output["POST.VALIDATION"];
            $redirectURL = $output["FRONTEND.REDIRECT_URL"];

            //$result = var_export($output, true);
            //file_put_contents('text.txt', $result, FILE_APPEND | LOCK_EX);
            // here you can get all variables reuturned from the ctpe server (see post integration transactions documentation for help)
            // everything ok, redirect to the WPF
            if ($processingresult == "ACK") {
                if (strstr($redirectURL, "http")) { // redirect url is returned ==> everything ok
//            echo " redirect url is returned ==> everything ok:" . $redirectURL;
                    header("Location: $redirectURL");
                } else { // error-code is returned ... failure
                    echo "error-code is returned ... failure";
//            header("Location: http://www.merchant.com/error.php");
                }
            } // there is a connection-problem to the ctpe server ... redirect to error page (change the URL to YOUR error page)
            else {
                echo "here is a connection-problem to the ctpe server";
                //header("Location: http://www.merchant.com/error.html");
            }
        }

        public function merchant_return_payment() {

            $returnvalue = isset($_POST['PROCESSING_RESULT']) ? $_POST['PROCESSING_RESULT'] : FALSE;
            if ($returnvalue) {
                if (strstr($returnvalue, "ACK")) {
//
                    // URL after successful transacvtion (change the URL to YOUR success page: e.g. return to shopping)
                    //$result = var_export($_POST, true);
                    //file_put_contents('text2.txt', $result, FILE_APPEND | LOCK_EX);
                    print lang_url('confirm_payment') . "/" . $_POST['FRONTEND_SESSION_ID'];
//                    log_message('error', var_dump($_POST));
                } else {
                    // URL error in transaction (change the URL to YOUR error page)
//            echo "NOK";
                    print lang_url('error_payment') . "/" . $_POST['FRONTEND_SESSION_ID'];
                    ;
                }
            } else {
                echo "not allowed";
            }
        }

        public function error_payment_accomm($reservation_id) {

            //cancelReservation
            $answer = $this->rest->post("cancelReservation", array('reservation_ID' => $reservation_id));

            $data['seo_title'] = lang('title-confirmation');
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

            $this->template->set_view('template_checkout');
            $this->template->load('site/payment_error', $data);
        }

        public function error_payment_exc($reservation_id) {
            //cancelReservation
            $answer = $this->rest->post("cancelReservationExcursions", array('reservation_ID' => $reservation_id));

            $data['seo_title'] = lang('title-confirmation');
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

            $this->template->set_view('template_checkout');
            $this->template->load('site/payment_error', $data);
        }

        public function error_payment_tr($reservation_id) {
            //cancelReservation
            $answer = $this->rest->post("cancelReservationTransfers", array('reservation_ID' => $reservation_id));

            $data['seo_title'] = lang('title-confirmation');
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

            $this->template->set_view('template_checkout');
            $this->template->load('site/payment_error', $data);
        }

        public function confirm_payment($reservation_id) {

            $parts = explode('-', $reservation_id);

            if (isset($parts[0])):
                switch ($parts[0]) {
                    case 'ac':
                        $this->confirm_payment_accomm($parts[1]);
                        break;

                    case 'ex':
                        $this->confirm_payment_exc($parts[1]);
                        break;

                    case 'tr':
                        $this->confirm_payment_tr($parts[1]);
                        break;
                }
            else:
                die('Page not found.');
            endif;
        }

        public function error_payment($reservation_id) {

            $parts = explode('-', $reservation_id);

            if (isset($parts[0])):
                switch ($parts[0]) {
                    case 'ac':
                        $this->error_payment_accomm($parts[1]);
                        break;

                    case 'ex':
                        $this->error_payment_exc($parts[1]);
                        break;

                    case 'tr':
                        $this->error_payment_tr($parts[1]);
                        break;
                }
            else:
                die('Page not found.');
            endif;
        }

        /*
         *
         * CC 4012888888881881
         *
         * Ovdje imamp dvije opcije
         *
         * Opciija I: 100%
         * Opcija II: 30% - 70%
         *
         */

        private function confirm_payment_accomm($reservation_id) {

            $coockie = get_cookie('cipa_cookie'); // Redirect if page reload

            if (!$coockie)
                redirect(lang_url());

            $this->load->helper('percent');
            $data['reservation'] = new stdClass();

            $data['reservation']->reservation = $this->rest->post("showReservationInfo", array('reservation_id' => $reservation_id));

            /* Prvo pronalazimo payment amount */
            $payment_info = $this->calculate_payment_amount($data['reservation']);


            $rest_data = array(
                'amount' => $payment_info['amount'],
                'agency' => 'cipa',
                'reservation_ID' => $reservation_id,
                'FULL_PAYMENT' => $payment_info['FULL_PAYMENT']
            );

            $result = $this->rest->post("confirm_payment", $rest_data);

            delete_cookie('cipa_cookie');

            $data['seo_title'] = lang('title-confirmation');
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

            $this->template->set_view('template_checkout');
            $this->template->load('site/payment_thanks', $data);
        }

        private function confirm_payment_exc($reservation_id) {

            $coockie = get_cookie('cipa_cookie'); // Redirect if page reload

            if (!$coockie)
                redirect(lang_url());

            $rest_data = array(
                'agency' => 'cipa',
                'reservation_ID' => $reservation_id
            );

            $result = $this->rest->post("confirm_paymentExcursions", $rest_data);

            delete_cookie('cipa_cookie');

            $data['seo_title'] = lang('title-confirmation');
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

            $this->template->set_view('template_checkout');
            $this->template->load('site/payment_thanks', $data);
        }

        private function confirm_payment_tr($reservation_id) {

            $coockie = get_cookie('cipa_cookie'); // Redirect if page reload

            if (!$coockie)
                redirect(lang_url());

            $rest_data = array(
                'agency' => 'cipa',
                'reservation_ID' => $reservation_id
            );

            $result = $this->rest->post("confirm_paymentTransfers", $rest_data);

            delete_cookie('cipa_cookie');

            $data['seo_title'] = lang('title-confirmation');
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

            $this->template->set_view('template_checkout');
            $this->template->load('site/payment_thanks', $data);
        }

        private function calculate_payment_amount($reservation) {

            $this->load->helper('percent');

            /*
             * Ovdje provjeravamo da li je opcija I ili opcija II
             */

            $reservation = $reservation->reservation->reservation;

            $payment_info = array(
                'amount' => 0,
                'FULL_PAYMENT' => FALSE,
            );

            if ($reservation->payment_options == PAYMENT_OPTION_30_70_1 || $reservation->payment_options == PAYMENT_OPTION_30_70_2):

                $payment_info['amount'] = find_percent(30, $reservation->price_total);
                $payment_info['FULL_PAYMENT'] = TRUE;

            elseif ($reservation->payment_options == PAYMENT_OPTION_100):

                $payment_info['amount'] = $reservation->price_total;
                $payment_info['FULL_PAYMENT'] = TRUE;


            else:

                $payment_info['amount'] = 0;
                $payment_info['FULL_PAYMENT'] = FALSE;

            endif;

            return $payment_info;
        }

        /*
         * Question
         */

        public function question() {

            $_GET['agency'] = 'cipa';

            $answer = $this->rest->post("gotQuestion", $_GET);

            echo json_encode($answer);
        }

        /*
         * Subscribe
         */

        public function subscribe() {

            $answer = $this->rest->post("subscribe", $_GET);

            echo json_encode($answer);
        }

        /*
         * Show Reservation Cancellation
         */

        public function show_cancellation() {

            $this->template->set_view('template_other');
            $data['seo_title'] = "Reservation cancellation";
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";
            $this->template->load('parts/cancel', $data);
        }

        /*
         * Show Reservation Cancellation Thanx
         */

        public function show_cancellation_tnx() {

            //cancelReservation
            $answer = $this->rest->post("cancelReservation", $_GET);

            $this->template->set_view('template_other');
            $data['seo_title'] = "Reservation cancellation";
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";
            $this->template->load('parts/thanks', $data);
        }

        /*
         * Show Review Form
         */

        public function show_review_form() {

            //reservation info
            $rest_data = array(
                'reservation_id' => $_GET['reservation_ID']
            );
            $data['reservation'] = $this->rest->post("showReservationInfo", $rest_data);

            $this->template->set_view('template_other');
            $data['seo_title'] = "User review";
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

            $this->template->load('parts/review_form', $data);
        }

        /*
         * Submit Review Form
         */

        public function subit_review_form() {

            //cancelReservation
            $this->rest->post("submitReview", $_GET);

            $this->template->set_view('template_other');
            $data['seo_title'] = "User review";
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";
            $this->template->load('parts/thanks', $data);
        }

        private function _load_top_offer_CMS(&$data) {

            $this->load->model('icms');

            $this->load->library('factory');
            $this->translate = $this->factory->create('translate');

            $data['top_offer'] = $this->findCategoryPosts(4);
        }

        protected function findCategoryPosts($ID, $limit = NULL, $order = NULL, $parent = NULL, $unset = NULL) {

            $category = $this->translate->res_array($this->icms->find_by_cat($ID, $order, $limit, $parent), $this->activeLang);
            foreach ($category as &$value):

                $photo = $this->icms->find_top_photo($value['ID']);

                $value['photo_name'] = $photo['filename'];
                $value['photo_alttext'] = $photo['alttext'];

            endforeach;

            if (isset($unset)):

                $cnt = count($category);
                for ($i = 0; $i < $cnt; $i++):
                    if ($category[$i]["ID"] == $unset):
                        unset($category[$i]);

                    endif;
                endfor;

                $category = array_values($category);

            endif;

            url_cms_name($category, 'title');

            return $category;
        }

        public function confirmationExcursions() {

            $coockie = get_cookie('cipa_cookie'); // Redirect if page reload
            if (!$coockie)
                redirect(lang_url());

            $rest_data = $_GET;
            $rest_data['agency'] = 'cipa';

            $data['reservation'] = $this->rest->post("createReservationExcursions", $rest_data);

            /*
             * Ovdje idemo na placanje preko payment gateway-a ako je:
             *
             *  -> metod placanja - Credit Card online
              -> selling type - Real Time
             */
//            if ($_GET['payment_method'] === CREDIT_CARD_ONLINE):
//
//                // GO TO PAYMENT GATEWAY METHOD
//                $payment_info['amount'] = $data['reservation']->reservation->totalprice;
//                $this->go_to_payment($data['reservation']->reservation_id, $payment_info['amount'], 'ex-');
//
//                return;
//
//            endif;

            delete_cookie('cipa_cookie');

            $data['seo_title'] = lang('title-confirmation');
            $data['seo_description'] = "";
            $data['seo_keywords'] = "";

            $this->template->set_view('template_checkout');
            $this->template->load('site/checkout_thanks_excursions', $data);
        }

        public function confirmationTransfers() {

            $coockie = get_cookie('cipa_cookie'); // Redirect if page reload
            if (!$coockie)
                redirect(lang_url());

            $rest_data = $_GET;
            $rest_data['agency'] = 'cipa';

            $data['reservation'] = $this->rest->post("createReservationTransfers", $rest_data);

            /*
             * Ovdje idemo na placanje preko payment gateway-a ako je:
             *
             *  -> metod placanja - Credit Card online
              -> selling type - Real Time
             */
            if ($_GET['payment_method'] === CREDIT_CARD_ONLINE):

                // GO TO PAYMENT GATEWAY METHOD
                $payment_info['amount'] = $data['reservation']->reservation->totalprice;
                $this->load->helper('percent');

                if ($_GET['payment_options'] == PAYMENT_OPTION_30_70_1 || $_GET['payment_options'] == PAYMENT_OPTION_30_70_2)
                    $payment_info['amount'] = find_percent(30, $data['reservation']->reservation->totalprice);
                else
                    $payment_info['amount'] = $data['reservation']->reservation->totalprice;



                $this->go_to_payment($data['reservation']->reservation_id, $payment_info['amount'], 'tr-');

                return;

            endif;

//            delete_cookie('cipa_cookie');
//
//            $data['seo_title'] = lang('title-confirmation');
//            $data['seo_description'] = "";
//            $data['seo_keywords'] = "";
//
//            $this->template->set_view('template_checkout');
//            $this->template->load('site/payment_thanks', $data);
        }

    }
    